package cky_interface;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

import cky_parser.CKYTable;

public class iTable extends JPanel {
	public iTable(int rowNumber, int colNumber, String[] words){
		this.setLayout(new GridLayout(rowNumber + 1,colNumber));
		this.setBackground(Color.black);
		
		for(int i=0;i<=colNumber;i++){
			JPanel pn = new JPanel();
			JLabel lb;
			JLabel lb2;
			if(i==0){
				lb = new JLabel(".",JLabel.CENTER);
				lb2 = new JLabel("",JLabel.CENTER);
			}
			else{
				lb = new JLabel(Integer.toString(i-1),JLabel.CENTER);
				lb2= new JLabel( words[i-1],JLabel.CENTER);
			}
			pn.setLayout(new GridLayout(2,0));
			pn.setBackground(Color.black);
			pn.setBorder(new LineBorder(Color.black));
			lb.setForeground(Color.white);
			lb2.setForeground(Color.yellow);
			pn.add(lb2);
			pn.add(lb);
			this.add(pn);
		}
	}
	public void AddCellContent(String []string, boolean isDark)
	{
		iCell cell = new iCell();
		cell.UpdateContent(string,isDark);
		this.add(cell);
	}
	public void MatchTable(iTable table,CKYTable tab){
		boolean cellColour = false;
		for(int i=0;i<tab.words.length;i++){
			JLabel lb = new JLabel(Integer.toString(i),JLabel.CENTER);
			//lb.setBackground(Color.black);
			lb.setForeground(Color.white);
			lb.setBorder(new LineBorder(Color.black));
			table.add(lb);
			for(int j=0;j<tab.words.length;j++){
				table.AddCellContent(tab.getContent(i, j), cellColour);
				cellColour = !cellColour;
			}
		}
	}
}
class iCell extends JPanel {
	public iCell(){
		this.setLayout(new GridLayout(2,1));				
	}
	public void UpdateContent(String []string, boolean isDark){
		if(isDark == true)
			this.setBackground(Color.white);
		else
			this.setBackground(Color.lightGray);

		this.setBorder(new LineBorder(Color.black));
		
		JLabel lb = new JLabel("");
		JPanel pn = new JPanel();
		if( string == null)
		{
			this.add(lb);
			return;
		}
		// Set cell coordiante
		lb = new JLabel(string[1],JLabel.CENTER);
		lb.setBackground(this.getBackground());
		this.add(lb);
		
		// Set cell content
		pn.setLayout(new GridLayout());
		pn.setBackground(this.getBackground());
		
		for(int i = 2;i<string.length;i++){
			if(string[i] == null)
				break;
			lb = new JLabel(string[i],JLabel.CENTER);
			lb.setBackground(this.getBackground());
			if(Integer.parseInt(string[0]) == (i - 2))
				lb.setForeground(Color.red);
			pn.add(lb);
		}
		this.add(pn);
		//lb.setLocation(new Point(0,0));
		//lb.setSize(new Dimension(50,20));
	}
}

